import React, {Component} from 'react';
import './index.less';
import getTreeRoot from './functions/getTreeRoot';
import getChildTree from './functions/getChildTreeData';

// 请求过子树数据的节点id存放在这里
// 每次请求前检查此数组，来判断是否请求过，请求过的不再请求
const hasFetchNode = {
    1: [],
    2: []
};

class LeftContent extends Component {
    constructor(props) {
        super(props);

        this.getTreeRootData = this.getTreeRootData.bind(this);
        this.selectNode = this.selectNode.bind(this);
        this.loadTreeData = this.loadTreeData.bind(this);
    }

    loadTreeData(pk, treeNode) {
        const {
            pk_org,
            asyncTree,
            treeId,
            queryModel
        } = this.props;
        // 此处是自定义的根节点，所以不请求
        if (pk === 'custom_root' || hasFetchNode[queryModel].indexOf(pk) >= 0) {
            return;
        }
        hasFetchNode[queryModel].push(pk);

        getChildTree({
            pk_org: pk_org,
            tree_node_id: pk,
            includeOtherNode: queryModel === '1'
        })
            .then((res) => {
                if (res.success) {
                    asyncTree.setTreeData(treeId, res.data, treeNode);
                }
            });
    }

    selectNode(pk) {
        this.props.onSelect(pk && pk[0]);
    }

    componentWillReceiveProps(nextProps) {
        // 此处判断如果当前的refpk和下一个refpk不同，就请求左树根节点数据
        if (nextProps.pk_org !== this.props.pk_org) {
            this.getTreeRootData(nextProps.pk_org);
        }
        if (nextProps.json['hi6007-000064'] !== this.props.json['hi6007-000064']) {
            setTimeout(() => {
                this.getTreeRootData(nextProps.pk_org);
            })
        }
    }

    getTreeRootData(pk_org) {
        const {
            treeId,
            asyncTree,
            queryModel
        } = this.props;
        let root = [{
            children: [],
            code: this.props.json['hi6007-000064'], /* 国际化处理： 管理范围*/
            iconBox: {
                addIcon: true,
                delIcon: true,
                editIcon: true
            },
            id: null,
            isOrgStruSort: false,
            innercode: null,
            key: 'root',
            name: this.props.json['hi6007-000064'], /* 国际化处理： 管理范围*/
            nodeData: {
                nodeValue: {}
            },
            pid: null,
            refname: this.props.json['hi6007-000064'], /* 国际化处理： 管理范围*/
            refpk: 'custom_root',
            title: this.props.json['hi6007-000064']/* 国际化处理： 管理范围*/
        }];
        if (!pk_org) {
            asyncTree.setTreeData(treeId, root);
            return;
        }
        getTreeRoot({
            pk_org, queryModel
        })
            .then((res) => {
                root[0].children = res.data;
                asyncTree.setTreeData(treeId, root);
                asyncTree.openNodeByPkAsync(treeId, 'custom_root');
            });
    }

    render() {
        const {asyncTree, treeId} = this.props;

        return (
            <div className='left-tree'>
                {asyncTree.createAsyncTree({
                    treeId: treeId,
                    needSearch: false,
                    needEdit: false,
                    showLine: true,
                    loadTreeData: this.loadTreeData,
                    onSelectEve: this.selectNode
                })}
            </div>
        );
    }
}

export default LeftContent;
